package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Permission;
import com.itheima.health.service.PermissionService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName RoleContoller
 * @Description TODO
 * @Author ly
 * @Company 深圳黑马程序员
 * @Date 2020/2/13 16:04
 * @Version V1.0
 */
@RestController
@RequestMapping(value = "/permission")
public class PermissionContoller {

    @Reference// 订阅 dubbo注解
    PermissionService permissionService;

    //分页查询
    @RequestMapping("/findPage")
    //@PreAuthorize(value = "")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = permissionService.pageQuery(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return pageResult;
    }

    //新增权限
    @RequestMapping("/add")
    //@PreAuthorize(value = "hasAuthority('')")
    public Result add(@RequestBody Permission permission){

        try {
            permissionService.add(permission);
            return new Result(true, MessageConstant.ADD_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_PERMISSION_FAIL);
        }


    }

    //编辑(先根据id查询数据再编辑修改)
    // 跳转到权限编辑页面
    @RequestMapping("/findById")
    public Result findById(Integer id){
        Permission permission = permissionService.findById(id);
        if(permission!=null){
            return new Result(true,MessageConstant.QUERY_PERMISSION_SUCCESS,permission);
        }
        else{
            return new Result(false,MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }

    //编辑保存
    @RequestMapping("/edit")
    //@PreAuthorize(value = "hasAuthority('CHECKITEM_EDIT')")
    public Result edit(@RequestBody Permission permission){
        try {
            permissionService.edit(permission);
            return new Result(true,MessageConstant.EDIT_PERMISSION_SUCCESS);
        }catch (Exception e){
            return new Result(false,MessageConstant.EDIT_PERMISSION_FAIL);
        }

    }

    //删除
    @RequestMapping("/delete")
    //@PreAuthorize(value = "hasAuthority('CHECKITEM_DELETE123')")
    public Result delete(Integer id){
        try {
            permissionService.delete(id);
            return new Result(true,MessageConstant.DELETE_PERMISSION_SUCCESS);
        }catch (RuntimeException e){
            return new Result(false,e.getMessage());
        }catch (Exception e){
            return new Result(false, MessageConstant.DELETE_PERMISSION_FAIL);
        }

    }

    
}
